package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class CustomerController {
    @Autowired
    CustomerMapper customerMapper;

    @RequestMapping("/customers")
    public String list(Model model){
        List<Customer> customers = customerMapper.queryCustomers();
        model.addAttribute("customers",customers);
        return "customers/list";
    }

    @GetMapping("/customer")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Customer> customers = customerMapper.queryCustomers();
        model.addAttribute("customers",customers);
        return "customers/add";
    }

    @PostMapping("/customer")
    public String addEmp(Customer customer){
        customer.setCid(customerMapper.getMId());
        customerMapper.addCustomer(customer);
        return "redirect:/customers";
    }

    //去客户的修改页面
    @GetMapping("/customer/{id}")
    public String toUpdateCustomer(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Customer customer = customerMapper.queryCustomerById(id);
        model.addAttribute("customer", customer);
        return "customers/update";
    }

    @PostMapping("/updateCustomer")
    public String updateCustomer(Customer customer) {
        System.out.println(customer.getAddress());
        customerMapper.updateCustomer(customer);
        return "redirect:/customers";
    }


    @GetMapping("/deleteCustomer/{id}")
    public String deleteCustomer(@PathVariable("id")Integer id){
        customerMapper.deleteCustomer(id);
        return "redirect:/customers";
    }

}
